import { Component, OnInit } from '@angular/core';
// 在组建中导入表单处理模块
// import { NgForm } from '@angular/forms';

// 导入路由服务
import { Router, ActivatedRoute, ActivatedRouteSnapshot } from '@angular/router';

// 导入本地存储服务
import { StorageService } from '../../service/storage.service';

@Component({
  selector: 'app-tag-edit',
  templateUrl: './tag-edit.component.html',
  styleUrls: ['./tag-edit.component.css']
})
export class TagEditComponent implements OnInit {
  // 标签列表
  private tagList: {
    id: number,
    name: string
  }[];
  // 当前标签
  private target: { id: number, name: string };

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private storage: StorageService
  ) {
   
  }

  ngOnInit() {
     // 从本地存储中读取tagList
     this.tagList = this.storage.tagList;
     // 获取路由中的查询参数
     const id = this.route.snapshot.queryParams.id;
     this.target = this.tagList.find(item => {
       return item.id == id
     });

  }

  // 编辑处理方法
  editHandle(f): void {
    if (!f.valid) {
      alert('请填写完整的表单信息');
      return;
    }
    this.storage.tagEdit(this.target.id,this.target.name);
    alert('操作成功');
    this.router.navigate(['/taglist'],{queryParams:{id:this.target.id}});
  }

}
